package top.yunmouren.areamusic.config;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//



import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import top.yunmouren.areamusic.AreaMusic;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class ConfigManager {
    private final AreaMusic plugin;
    private Map<String, Map<String, AreaDto>> areas;
    private Map<String, MusicDto> musics;

    public ConfigManager(AreaMusic plugin) {
        this.plugin = plugin;
    }

    public void load() throws IOException {
        this.reload();
    }

    public void reload() throws IOException {
        this.plugin.saveDefaultConfig();
        this.plugin.reloadConfig();
        if (!this.plugin.getDataFolder().exists()) {
            this.plugin.getDataFolder().mkdir();
        }

        File areaFile = new File(this.plugin.getDataFolder(), "area.json");
        if (!areaFile.exists()) {
            areaFile.createNewFile();
            FileWriter fileWriter = new FileWriter(areaFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("{}");
            bufferedWriter.close();
            fileWriter.close();
            this.areas = new HashMap();
        } else {
            FileReader fileReader = new FileReader(areaFile);
            Gson gson = new Gson();
            Type areasType = (new TypeToken<HashMap<String, HashMap<String, AreaDto>>>() {
            }).getType();
            this.areas = (Map)gson.fromJson(fileReader, areasType);
            fileReader.close();
        }

        File musicFile = new File(this.plugin.getDataFolder(), "music.json");
        if (!musicFile.exists()) {
            musicFile.createNewFile();
            FileWriter fileWriter = new FileWriter(musicFile);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("{}");
            bufferedWriter.close();
            fileWriter.close();
            this.musics = new HashMap();
        } else {
            FileReader fileReader = new FileReader(musicFile);
            Gson gson = new Gson();
            Type musicsType = (new TypeToken<HashMap<String, MusicDto>>() {
            }).getType();
            this.musics = gson.fromJson(fileReader, musicsType);
            fileReader.close();
        }

    }

    public void save() throws IOException {
        this.plugin.saveConfig();
        File areaFile = new File(this.plugin.getDataFolder(), "area.json");
        FileWriter fileWriter;
        if (!areaFile.exists()) {
            areaFile.createNewFile();
            fileWriter = new FileWriter(areaFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);
            writer.write("{}");
            writer.close();
            fileWriter.close();
        } else {
            fileWriter = new FileWriter(areaFile);
            Gson gson = new Gson();
            gson.toJson(this.areas, fileWriter);
            fileWriter.close();
        }

        File musicFile = new File(this.plugin.getDataFolder(), "music.json");
        if (!musicFile.exists()) {
            musicFile.createNewFile();
            fileWriter = new FileWriter(musicFile);
            BufferedWriter writer = new BufferedWriter(fileWriter);
            writer.write("{}");
            writer.close();
            fileWriter.close();
        } else {
            fileWriter = new FileWriter(musicFile);
            Gson gson = new Gson();
            gson.toJson(this.musics, fileWriter);
            fileWriter.close();
        }

    }

    public void unload() throws IOException {
        this.save();
    }

    public void setCheckPeriod(Integer checkPeriod) {
        this.plugin.getConfig().set("engine.checkPeriod", checkPeriod);
    }

    public Integer getCheckPeriod() {
        return this.plugin.getConfig().getInt("engine.checkPeriod");
    }

    public Map<String, Map<String, AreaDto>> getAreas() {
        return this.areas;
    }

    public void setAreas(Map<String, Map<String, AreaDto>> areas) {
        this.areas = areas;
    }

    public Map<String, MusicDto> getMusics() {
        return this.musics;
    }

    public void setMusics(Map<String, MusicDto> musics) {
        this.musics = musics;
    }
}
